<template>
  <div>
    <!-- 卡片组件 -->
    <el-card class="box-card">
      <UploadExcel :on-success="onSuccess" />
    </el-card>
  </div>
</template>

<script>
// 时间处理函数
import { formatExcelDate } from '@/utils'
import { excelEmployeeAPI } from '@/api/employees'
export default {
  name: 'Excel',
  data() {
    return {

    }
  },
  methods: {
    async  onSuccess({ header, results }) {
      const data = this.transResults(results)
      await excelEmployeeAPI(data)
      this.$router.back()
    },
    transResults(results) {
      // 枚举数据
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      return results.map(item => {
        //   定义空对象来保存每次转换后的数据
        const obj = {}
        // 取出属性名数组
        const keyList = Object.keys(item)
        keyList.forEach(key => {
          // 将属性名格式化
          const transKey = userRelations[key]
          if (transKey === 'timeOfEntry' || transKey === 'correctionTime') {
            obj[transKey] = new Date(formatExcelDate(item[key]))
          } else {
            obj[transKey] = item[key]
          }
        })
        return obj
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
